package com.xiaxueqi.web.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.niagara.context.SecurityContext;
import com.niagara.dao.Page;
import com.niagara.security.auth.entity.UserDetail;
import com.niagara.service.base.BaseService;
import com.niagara.web.struts2.CRUDActionSupport;
import com.niagara.web.struts2.Struts2Utils;
import com.xiaxueqi.entity.book.AppFavorite;
import com.xiaxueqi.entity.book.BookStore;

@Results( { @Result(name = CRUDActionSupport.RELOAD, location = "favorite-book.action", type = "redirect") })
public class FavoriteBookAction extends CRUDActionSupport<AppFavorite> {
	private static final long serialVersionUID = 1L;

	@Autowired
	private BaseService baseService;
	private AppFavorite entity;
	private Integer id;
	private Page<AppFavorite> page;

	private UserDetail detail = SecurityContext.getUserDetail();
	private Integer userId = detail != null ? Integer.valueOf(detail
			.getUserId().intValue()) : null;
	private List<BookStore> books = new ArrayList<BookStore>();
	private Integer number;

	private Integer bookId;

	public void setBookId(Integer bookId) {
		this.bookId = bookId;
	}

	public Integer getNumber() {
		return this.number;
	}

	public List<BookStore> getBooks() {
		return books;
	}

	public Page<AppFavorite> getPage() {
		return page;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	// 用户自己收藏列表的逻辑删除
	@Override
	public String delete() throws Exception {
		this.entity = this.baseService.get(AppFavorite.class, this.id);
		this.entity.setDeleted("Y");
		this.baseService.update(this.entity);
		return RELOAD;
	}

	// 用户自己图书收藏列表，可以管理自己收藏图书
	@SuppressWarnings("unchecked")
	@Override
	public String list() throws Exception {
		Map map = Struts2Utils.getParameters();
		Integer pageNo = Struts2Utils.getIntParameter(PARAMS_PAGENO, Integer
				.valueOf(1));
		map.put("ownerId", userId);// 取未被删除的记录
		map.put("favoriteType", "book");
		/*
		 * System.out.println(map); 用浏览器地址栏检验，map可以取到所有的参数，
		 * 但是下面的map查询只会用到与其字段相关的参数
		 */
		this.page = this.baseService.findForPaging(AppFavorite.class, map,
				pageNo, DEFAULT_PAGE_SIZE);
		List<AppFavorite> list = this.page != null ? this.page.getPageList()
				: new ArrayList<AppFavorite>();
		for (int i = 0; i < list.size(); i++) {
			this.books.add(this.baseService.get(BookStore.class, list.get(i)
					.getFavoriteId()));
		}
		Map m = new HashMap();
		m.put("ownerId", userId);

		// 下面这里实际执行是ID为select_paging_count_by_map的sql语句， 框架实现时会自动加上_count_by_map
		this.number = this.baseService.count(AppFavorite.class, m,
				"select_paging");
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (this.id != null) {
			this.entity = this.baseService.get(AppFavorite.class, this.id);
		} else {
			this.entity = new AppFavorite();
		}
	}

	// 添加收藏的图书
	@Override
	public String save() throws Exception {
		if (entity.getId() == null) {
			baseService.insert(entity);
		} else {
			baseService.update(entity);
		}
		addActionMessage("保存成功");
		return RELOAD;
	}

	public AppFavorite getModel() {
		return this.entity;
	}

	public String add() throws Exception {
		this.entity = new AppFavorite();
		entity.setFavoriteId(bookId);
		AppFavorite tempEntity = this.baseService.get(AppFavorite.class,
				entity, "select_by_condition");
		entity.setDeleted("N");
		entity.setFavoriteType("book");
		entity.setOwnerId(userId);
		if (tempEntity == null) {
			baseService.insert(entity);
		} else {
			Struts2Utils.renderText("对不起，此书您已经收藏过！");
		}

		return NONE;
	}

}
